package cn.java71.service;

import cn.java71.dao.StudentDao;
import cn.java71.entity.Page;
import cn.java71.entity.Student;
import cn.java71.util.MyBatisUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
//封装db操作
public class StudentService {
    public boolean checkTelephone(String phone){
        {
            SqlSession sqlSession = MyBatisUtil.get();
            try {
                StudentDao dao = sqlSession.getMapper(StudentDao.class);
                return dao.checkTelephone(phone)>0;//true表示号码存在了
            }finally {
                MyBatisUtil.close(sqlSession);//模型
            }
        }
    }
    public Student get(long stu){
        SqlSession sqlSession = MyBatisUtil.get();
        try {
            StudentDao dao = sqlSession.getMapper(StudentDao.class);
            return dao.get(stu);
        }finally {
            MyBatisUtil.close(sqlSession);//模型
        }
    }

    /**
     * 学生信息的分页查询的
     * @param pageNo
     * @param pageSize
     * @param sname
     * @param start
     * @param end
     * @return Page对象
     */
    public Page<Student>  search(int pageNo,  int pageSize, String sname, Date start,  Date end){
        SqlSession sqlSession = MyBatisUtil.get();
        try {
            StudentDao dao = sqlSession.getMapper(StudentDao.class);
            List<Student> list = dao.search2(pageNo, pageSize, sname, start, end);
            int count=dao.searchTotal(sname,start,end);
            return new Page<Student>(list,pageNo,count,pageSize);
        }finally {
            MyBatisUtil.close(sqlSession);//模型
        }
    }
}
